package com.gxa.pingan.service.impl;

import com.gxa.pingan.common.ExceptionCode;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.mapper.EvaluateMapper;
import com.gxa.pingan.pojo.Evaluate;
import com.gxa.pingan.service.EvaluateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @version fi
 * @Author lonny
 * @Date 2021/10/26 14:05
 */
@Service(value = "EvaluateService")
@Transactional(rollbackFor = Exception.class)
public class EvaluateServiceImpl implements EvaluateService {
    @Resource
    private EvaluateMapper evaluateMapper;


    /**
     * 用户评价添加方法
     *
     * @param evaluate 对象
     */
    @Override
    public void addEvaluate(Evaluate evaluate) throws Exception {
        if (evaluate.getOrderNum() == null) {
            throw new BusinessException(ExceptionCode.PARAM_ERROR);
        }
        Evaluate byOrderNum = evaluateMapper.findByOrderNum(evaluate.getOrderNum());
        if (byOrderNum != null) {
            throw new BusinessException(ExceptionCode.REPEAT_UPDATE_STATUS);
        }
        //            默认评价时间为当前系统时间并且评价是可见状态
        evaluate.setIsUsed(true);
        evaluate.setAppraiseTime(new Timestamp(System.currentTimeMillis()));
        evaluateMapper.addEvaluate(evaluate);
    }

    /**
     * 评价查询方法 包含模糊查询
     * 通过下单时间逆序排序
     *
     * @param fromPayTime 下单时间至   大于输入时间
     * @param toPayTime   下单时间至 小于输入时间
     * @param page        页码
     * @param size        每页数量
     * @return 返回List 评价列表
     */
    @Override
    public List<Map<String, Object>> getEvaluateList(String object, String fromPayTime, String toPayTime, Integer page, Integer size) throws Exception {
        if (page != null) {
            if (page <= 0) {
                throw new BusinessException(ExceptionCode.PAGE_ERROR);
            }
            return evaluateMapper.getEvaluateList(object, fromPayTime, toPayTime, (page - 1) * size, size);
        }
        throw new BusinessException(ExceptionCode.PAGE_ERROR);
    }

    @Override
    public int size(String object, String fromPayTime, String toPayTime) {
        List<Map<String, Object>> evaluateList = evaluateMapper.getEvaluateList(object, fromPayTime, toPayTime, null, null);
        return evaluateList.size();
    }

    /**
     * 修改评价是否可见
     * 如果当前是不可见点击之后更改为可见,否之亦然
     *
     * @param id id
     */
    @Override
    public void updateEvaluate(String id) throws Exception {
        if (id == null || id.length() <= 0) {
            throw new BusinessException(ExceptionCode.PARAM_ERROR);
        }
        Evaluate byId = evaluateMapper.findById(id);
        if (byId != null) {
            evaluateMapper.updateEvaluate(id);
            return;
        }
        throw new BusinessException(ExceptionCode.NO_USER);
    }

    /**
     * 删除评价
     *
     * @param ids 通过id锁定
     * @return f
     */
    @Override
    public int deleteEvaluate(String ids) throws Exception {
        if (ids == null || ids.isEmpty()) {
            throw new BusinessException(ExceptionCode.PARAM_ERROR);
        }
        String[] id = ids.split(",");
        List<String> list = Arrays.asList(id);
        for (String s : list) {
            Evaluate byId = evaluateMapper.findById(s);
            System.out.println(byId);
            if (byId == null) {
                throw new BusinessException(ExceptionCode.NO_USER);
            }
        }
        return evaluateMapper.deleteEvaluate(list);
    }


    /**
     * 评价详情
     *
     * @param id 通过id锁定
     * @return 返回
     */
    @Override
    public Map<String, Object> details(String id) throws Exception {
        Map<String, Object> details = evaluateMapper.details(id);

        if (details != null) {
            return details;
        }
        throw new BusinessException(ExceptionCode.NO_USER);
    }
}
